//
//  LAB_FootView.m
//  Camera
//
//  Created by Bodi on 16/8/11.
//  Copyright © 2016年 葛海一. All rights reserved.
//

#import "LAB_FootView.h"
const static NSInteger btnTag = 100;
@implementation LAB_FootView

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/
- (instancetype)initWithFrame:(CGRect)frame{
    self = [super initWithFrame:frame];
    if (self) {
        [self addSubview:self.buttonView];
    }
    return self;

}
- (UIScrollView *)buttonView{
    if (!_buttonView) {
        _buttonView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, WIDTH, 49)];
        //是否回弹
        _buttonView.bounces = NO;
        //滚动条隐藏
        //水平
        _buttonView.showsHorizontalScrollIndicator = NO;
        //垂直
        _buttonView.showsVerticalScrollIndicator = NO;
    }
    return _buttonView;

}
- (void)addFootViewButton {
    for (NSUInteger idx = 0; idx < self.titleArr.count; idx ++) {
        UIButton * toolBtn = [[UIButton alloc] initWithFrame:CGRectMake(idx * WIDTH/self.titleArr.count, 0, WIDTH/self.titleArr.count, 47)];
        [toolBtn setTitle:self.titleArr[idx] forState:(UIControlStateNormal)];
        [toolBtn setTitleColor:[UIColor whiteColor] forState:(UIControlStateNormal)];
        [toolBtn setTitleColor:[UIColor orangeColor] forState:(UIControlStateSelected)];
        toolBtn.backgroundColor = [UIColor blackColor];
        toolBtn.titleLabel.font = [UIFont systemFontOfSize:17];
        toolBtn.tag = btnTag + idx;
        
        if (toolBtn.tag == btnTag) {
            //是否当前选中
            toolBtn.selected = YES;
            self.seletedBtn = toolBtn;
        }
        
        [toolBtn addTarget:self action:@selector(toolBtnClick:) forControlEvents:(UIControlEventTouchUpInside)];
        
        [self.buttonView addSubview:toolBtn];
    }
}
//按钮下的红条 button点击方法

- (void)toolBtnClick:(UIButton *)button {
    self.seletedBtn.selected = NO;
    button.selected = YES;
    self.seletedBtn = button;
    
    //  改变导航条的位置
    CGRect lineRect = self.line.frame;
    lineRect.origin.x = (WIDTH/self.titleArr.count) * (button.tag - btnTag);
    [UIView animateWithDuration:.2 animations:^{
        self.line.frame = lineRect;
    }];
    //指示是否接收机实现继承的方法可以指定消息做出反应
    if ([_delegate respondsToSelector:@selector(buttonDidSeletedWithIndex:)]) {
        //协议方法
        [_delegate buttonDidSeletedWithIndex:(button.tag - btnTag)];
    }
    
}

//  设置导航条
- (void)showLineWithButton {
    self.line = [[UILabel alloc] initWithFrame:CGRectMake(0, 47, WIDTH/self.titleArr.count, 2)];
    self.line.backgroundColor = [UIColor orangeColor];
    [self.buttonView addSubview:self.line];
}

@end
